﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 第二次作业.entity
{
    internal class ATM
    {
        public Bank bank;

        public event EventHandler<BigMoneyArgs>? BigMoneyFetched;

        public ATM()
        {
            this.bank = new Bank();
        }

        public void withdrawMoney(string user, double amount)
        {
            Account account = bank[user];

            if (account == null)
            {
                throw new InvalidOperationException("账户不存在！");
            }

            Random random = new Random();
            if (random.Next(0, 10) < 3)
            {
                throw new BadCashException("取款失败：遇到坏钞票！");
            }

            if (amount > 10000)
            {
                OnBigMoneyFetched(account.accountName, amount);
            }
            account.withdraw(amount);
        }

        public void depositMoney(string user, double amount)
        {
            Account account = bank[user];

            if (account == null)
            {
                throw new InvalidOperationException("账户不存在！");
            }

            account.deposit(amount);
        }

        protected virtual void OnBigMoneyFetched(string accountName, double amount)
        {
            BigMoneyArgs args = new BigMoneyArgs(accountName, amount);

            BigMoneyFetched?.Invoke(this, args);
        }

    }
}
